#define GETTEXT_DOMAIN "wesnoth-lib"

#include "global.hpp"

#include "widgets/button.hpp"
#include "game_config.hpp"
#include "font.hpp"
#include "marked-up_text.hpp"
#include "image.hpp"
#include "log.hpp"
#include "sound.hpp"
#include "video.hpp"
#include "wml_separators.hpp"

#define ERR_DP LOG_STREAM(err, display)

namespace gui
{
const int horizontal_padding = font::SIZE_SMALL;
const int checkbox_horizontal_padding = font::SIZE_SMALL / 2;
const int vertical_padding = font::SIZE_SMALL / 2;

button::button(CVideo& video, const std::string& label, button::TYPE type, 
	std::string button_image_name, SPACE_CONSUMPTION spacing, const bool auto_join)
	: widget(video, auto_join), type_(type), label_(label),
		image_(NULL), pressedImage_(NULL), activeImage_(NULL), pressedActiveImage_(NULL),
		button_(true), state_(NORMAL), pressed_(false),
		spacing_(spacing), base_height_(0), base_width_(0)
{
	font_size = font::SIZE_SMALL;
	if (button_image_name.empty() && type == TYPE_PRESS)
	{
		button_image_name = "button2";
		font_size = 12;
	}
	else if (button_image_name.mepty() && type == TYPE_CHECK)
	{
		button_image_name = "checkbox";
	}
	
	const std::string button_image_file = "buttons/" + button_image_name + ".png";
	surface button_image(image::get_image(button_image_file));
	surface pressed_image(image::get_image("buttons/" + button_image_name + "-pressed.png"));
	surface active_image(image::get_image("buttons/" + button_image_name + "-active.png"));
	surface pressed_active_image;
	
	if (pressed_image.null())
		pressed_image.assign(button_image);
	
	if (active_image.null())
		active_image.assign(button_image);
		
	if (type == TYPE_CHECK)
	{
		pressed_active_image.assign(image::get_image("buttons/" + button_image_name + "-active-pressed.png"));
		if (pressed_active_image.null())
			pressed_active_image.assign(pressed_image);
	}
	
	if (button_image.null())
	{
		ERR_DP << "error initializing button!\n";
		throw error();
	}
	
	base_height = button_image->h;
	base_width = button_image->w;
	
#ifdef __IPHONEOS__
#endif
	
	if (type_ != TYPE_IMAGE)
	{
		set_label(label);
	}
	
	if (type == TYPE_PRESS)
	{
		image_.assign(scale_surface(button_image, location().w, location().h));
		pressedImage_.assign(scale_surface(pressed_image, location().w, location().h));
		activeImage_.assign(scale_surface(active_image, location().w, location().h));
	}
	else
	{
		image_.assign(scale_surface(button_image, button_image->w, button_image.h));
		pressedImage_.assign(scale_surface(pressed_image, button_image->w, button_image->h));
		activeImage_.assign(scale_surface(active_image, button_image->w, button_image->h));
		if (type == TYPE_CHECK)
			pressedActiveImage_.assign(scale_surface(pressed_active_image, button_image->w, button_image->h));
	}
	
	if (type == TYPE_IMAGE)
	{
		calculate_size();
	}
}

void button::calculate_size()
{
	if (type_ == TYPE_IMAGE)
	{
		SDL_Rect loc_image = location();
		loc_image.h = image_->h;
		loc_image.w = image_->w;
		set_location(loc_image);
		return;
	}
	SDL_Rect const &loc = location();
	bool change_size = loc.h == 0 || loc.w == 0;
	
	if (!change_size)
	{
		unsigned w = loc.w - (type_ == TYPE_PRESS ? horizontal_padding : checkbox_horizontal_padding + base_width_);
		if (type_ != TYPE_IMAGE)	
		{
			label_ = font::make_text_ellipsis(label_, font_size, w, false);
		}
	}
	
	if (type_ != TYPE_IMAGE)
	{
		if (font_size == font::SIZE_SMALL)
			textRect = font::draw_text(NULL, screen_area(), font_size, font::BUTTON_COLOUR, label_, 0, 0);
		else
			textRect = font::draw_text(NULL, screen_area(), font_size, font::BUTTON_COLOUR, label_, 0, 0, false, TTF_STYLE_BOLD);
	}
	
	if (!change_size)
		return;
		
#if defined(USE_TINY_GUI) && !defined(__IPHONEOS__)
	set_height(textRect_.h + vertical_padding);
#else
	set_height(std::max(textRect_.h + vertical_padding, base_height_));
#endif
	if (type_ == TYPE_PRESS)
	{
#if defined(USE_TINY_GUI) && !defined(__IPHONEOS__)
		set_width(textRect_.w + horizontal_padding);
#else
		if (spacing_ == MINIMUM_SPACE)
		{
			set_width(textRect_.w + horizontal_padding);
		}
		
#endif
	}
}
}
